package utils;

import controller.DBUtils;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;

public class SelectPhoto extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            String str = "select * from clothes";
            Connection connection = DBUtils.Connection();
            resp.setContentType("image/jpg");
            //1.图片路径

            //2.创建文件
            File file = new File(getServletContext().getRealPath("/photo.jpg"));
            // System.out.println(getServletContext().getRealPath("/photo.jpg"));
            //3.创建字节流
            ServletOutputStream outputStream = resp.getOutputStream();
            BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
            //4.读取图片并写入response中的直接输出流
            byte [] bytes = new byte[1024];
            int lens = 0;
            while((lens=bufferedInputStream.read(bytes))!=-1){
                outputStream.write(bytes,0,lens);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
}
